# coding: utf-8
# Author: legend
# @Time： 2020/8/27 下午10:05
from apps.resource.models import Service
from base.constant import ServiceStatus
from utils.messages.handler import BaseHandler
from websocket.messages import LatestDeviceProcessStatusResponse, MultiDeviceProcessStatusResponse


class LatestDeviceProcessStatusRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        device_id = kwargs.get('device_id')
        services = list(
            await Service.filter(device_id=device_id, status=ServiceStatus.RUNNING).values_list('id', flat=True))
        result_data = self.query_process_status(1, services)
        return {
            'data': result_data,
            'result_code': LatestDeviceProcessStatusResponse.ResultCode.NORMAL
        }


class MultiDeviceProcessStatusRequestHandler(BaseHandler):

    async def do(self, *args, **kwargs):
        device_id = kwargs.get('device_id')
        limit_number = kwargs.get('size')
        services = list(
            await Service.filter(device_id=device_id, status=ServiceStatus.RUNNING).values_list('id', flat=True))
        result_data = self.query_process_status(limit_number, services)
        return {
            'result_code': MultiDeviceProcessStatusResponse.ResultCode.NORMAL,
            'data': result_data
        }
